package question.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;
import com.makeramen.roundedimageview.RoundedImageView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;

import https.InterNetController;
import pay.activity.Pay;
import personalPage.activity.PersonalPage;
import question.adapter.QuestionDetailEvaluateAdapter;
import question.bean.QuestionDetailBean;
import common.BaseActivity;
import common.Constant;
import utils.MyLogUtils;
import view.MyImageLoader;
import volunteer.activity.Volunteer;

/**
 * Created by Administrator on 2017/4/9.
 * 问题详情(免费问题和别人看都用这个)
 */

@ContentView(R.layout.question_detail)
public class QuestionDetail extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView common_title;
    @ViewInject(R.id.question_detail_lv)
    private ListView question_detail_lv;
    @ViewInject(R.id.question_detail_comment_layout)
    private LinearLayout question_detail_comment_layout;
    //    @ViewInject(R.id.question_detail_share)
//    private ImageView question_detail_share;
    private QuestionDetailEvaluateAdapter questionDetailEvaluateAdapter;

    private View headerView;
    private RoundedImageView question_detail_header_questioner_ima;
    private ArrayList<QuestionDetailBean.DetailBean.QuestionDetailCommentBean> arrayList;

    private TextView question_detail_header_questioner_name;
    private TextView question_detail_header_questioner_detail;
    private TextView question_detail_header_questioner_content;
    private ImageView question_detail_header_answer_ima;
    private TextView question_detail_header_answer_content;
    private LinearLayout question_detail_header_zan_layout;
    private LinearLayout question_detail_header_comment_layout;
    private LinearLayout question_detail_header_answer_layout;
    private ImageView question_detail_header_answer_vip_ima1;
    private ImageView question_detail_header_answer_vip_ima;
    private TextView question_detail_header_zan;
    private TextView question_detail_header_comment;
    private ImageView question_detail_header_answer_ima1;
    private TextView question_detail_header_answer_name;
    private TextView question_detail_header_answer_introduce;
    private TextView question_detail_header_answer_more;
    private TextView question_detail_header_answer_no_comment;
    private LinearLayout question_detail_header_price_layout;
    private TextView question_detail_header_answer_content_pay;

    private QuestionDetailBean.DetailBean detail;

    private final int GETDATA = 0;
    private final int ZAN = 1;

    private int zan;//赞的数量
    private int comment;//评论的数量
    private boolean isPraised;//是否点赞
    private boolean hasPay;//是否已经给钱

    private String dialogId;
    private String type;//问题是热门点进来的还是我的提问点进来的 有hot/my两种情况

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    switch (msg.arg1) {
                        case GETDATA:
                            Gson gson = new Gson();
                            QuestionDetailBean questionDetailBean = gson.fromJson((String) msg.obj, QuestionDetailBean.class);
                            if (questionDetailBean != null) {
                                detail = questionDetailBean.getDetail();
                                if (detail != null) {
                                    MyImageLoader.showImage(detail.getQuestioner_headimgurl(), question_detail_header_questioner_ima);
                                    MyImageLoader.showImage(detail.getAnswerer_headimgurl(), question_detail_header_answer_ima);
                                    MyImageLoader.showImage(detail.getAnswerer_headimgurl(), question_detail_header_answer_ima1);

                                    question_detail_header_questioner_name.setText(detail.getQuestioner_name());
                                    question_detail_header_questioner_detail.setText(detail.getQuestioner_city() + "/" + detail.getQuestioner_prov());
                                    question_detail_header_questioner_content.setText(detail.getQuestion());
                                    question_detail_header_answer_content.setText(detail.getAnswer());
                                    switch (detail.getAnswerer_type()) {
                                        case Constant.VOLUNTEER_TYPE_VIP:
                                            // 付费问题
                                            question_detail_header_answer_vip_ima1.setVisibility(View.GONE);
                                            question_detail_header_answer_vip_ima.setVisibility(View.GONE);
                                            question_detail_header_answer_introduce.setText(detail.getBrief());
                                            question_detail_header_answer_more.setText(detail.getAttention() + "人关注 · 解答" + detail.getAnswered() + "个问题");//付费问题的简介跟免费的不一样
                                            if (detail.getIspay() == 0) {
                                                //没给钱啊笨蛋,不让你看这些东西
                                                hasPay = false;
                                                question_detail_header_price_layout.setVisibility(View.VISIBLE);
                                                question_detail_header_answer_content.setVisibility(View.GONE);
                                                question_detail_header_answer_content_pay.setVisibility(View.VISIBLE);
                                                question_detail_comment_layout.setVisibility(View.GONE);
                                            } else {
                                                hasPay = true;
                                                question_detail_header_price_layout.setVisibility(View.GONE);
                                                question_detail_header_answer_content.setVisibility(View.VISIBLE);
                                                question_detail_header_answer_content_pay.setVisibility(View.GONE);
                                                question_detail_comment_layout.setVisibility(View.VISIBLE);
                                            }
                                            break;
                                        case Constant.VOLUNTEER_TYPE_OFFICIAL:
                                            question_detail_header_answer_vip_ima1.setVisibility(View.VISIBLE);
                                            question_detail_header_answer_vip_ima.setVisibility(View.VISIBLE);
                                            question_detail_header_price_layout.setVisibility(View.GONE);
                                            question_detail_header_answer_content.setVisibility(View.VISIBLE);
                                            question_detail_header_answer_content_pay.setVisibility(View.GONE);
                                            question_detail_comment_layout.setVisibility(View.VISIBLE);
                                            //付费问题的简介跟免费的不一样
                                            question_detail_header_answer_introduce.setText(detail.getCollege());
                                            question_detail_header_answer_more.setText(detail.getMajor());
                                            break;
                                        case Constant.VOLUNTEER_TYPE_NORMAL:
                                            //免费问题
                                            question_detail_header_answer_vip_ima1.setVisibility(View.GONE);
                                            question_detail_header_answer_vip_ima.setVisibility(View.GONE);
                                            question_detail_header_price_layout.setVisibility(View.GONE);
                                            question_detail_header_answer_content.setVisibility(View.VISIBLE);
                                            question_detail_header_answer_content_pay.setVisibility(View.GONE);
                                            question_detail_comment_layout.setVisibility(View.VISIBLE);
                                            //付费问题的简介跟免费的不一样
                                            question_detail_header_answer_introduce.setText(detail.getCollege());
                                            question_detail_header_answer_more.setText(detail.getMajor());
                                        default:
                                            break;
                                    }
                                    if (0 == detail.getIs_praised()) {
                                        //未点赞
                                        isPraised = false;
                                        Drawable d = QuestionDetail.this.getResources().getDrawable(R.mipmap.college_topic_item_zan_ima);
                                        d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
                                        question_detail_header_zan.setCompoundDrawables(d, null, null, null);
                                    } else {
                                        isPraised = true;
                                        Drawable d = QuestionDetail.this.getResources().getDrawable(R.mipmap.college_topic_item_zan_ima1);
                                        d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
                                        question_detail_header_zan.setCompoundDrawables(d, null, null, null);
                                    }
                                    if ("未回复".equals(detail.getState())) {
                                        question_detail_comment_layout.setVisibility(View.GONE);
                                    } else {
                                        question_detail_comment_layout.setVisibility(View.VISIBLE);
                                    }
                                    question_detail_header_zan.setText(detail.getPraise());
                                    zan = Integer.parseInt(detail.getPraise());
                                    question_detail_header_comment.setText(detail.getComment());
                                    comment = Integer.parseInt(detail.getComment());
                                    question_detail_header_answer_name.setText(detail.getAnswerer_name());
                                    if (detail.getCommentList() != null) {
                                        if (detail.getCommentList().size() > 0) {
                                            question_detail_header_answer_no_comment.setVisibility(View.GONE);
                                            arrayList.clear();
                                            for (QuestionDetailBean.DetailBean.QuestionDetailCommentBean bean : detail.getCommentList()) {
                                                arrayList.add(bean);
                                            }
                                            questionDetailEvaluateAdapter.notifyDataSetChanged();
                                        } else {
                                            question_detail_header_answer_no_comment.setVisibility(View.VISIBLE);
                                        }
                                    }
                                }
                            }
                            break;
                        case ZAN:
                            try {
                                JSONObject jsonObject = new JSONObject((String) msg.obj);
                                String error_code = jsonObject.getString("err_code");
                                if ("0".equals(error_code)) {
                                    //赞
                                    zan++;
                                    isPraised = true;
                                    if (!TextUtils.isEmpty(question_detail_header_zan.getText().toString())) {
                                        question_detail_header_zan.setText("" + (Integer.parseInt(question_detail_header_zan.getText().toString()) + 1));
                                        Drawable d = QuestionDetail.this.getResources().getDrawable(R.mipmap.college_topic_item_zan_ima1);
                                        d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
                                        question_detail_header_zan.setCompoundDrawables(d, null, null, null);
                                    }
                                } else if ("1".equals(error_code)) {
                                    //取消赞
                                    zan--;
                                    isPraised = false;
                                    if (!TextUtils.isEmpty(question_detail_header_zan.getText().toString())) {
                                        question_detail_header_zan.setText("" + (Integer.parseInt(question_detail_header_zan.getText().toString()) - 1));
                                        Drawable d = QuestionDetail.this.getResources().getDrawable(R.mipmap.college_topic_item_zan_ima);
                                        d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
                                        question_detail_header_zan.setCompoundDrawables(d, null, null, null);
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dialogId = getIntent().getStringExtra("dialogId");
        type = getIntent().getStringExtra("type");
        initView();
        getDataFromInterNet(true);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        dialogId = intent.getStringExtra("dialogId");
        type = intent.getStringExtra("type");
        initView();
        getDataFromInterNet(true);
    }

    private void initView() {
        common_back.setOnClickListener(this);
        common_title.setText("问题详情");
        //TODO
        //记得显示
//        common_search.setImageResource(R.mipmap.college_more_ima);
//        common_search.setVisibility(View.GONE);
//        common_search.setOnClickListener(this);

        question_detail_comment_layout.setOnClickListener(this);
//        question_detail_share.setOnClickListener(this);

        headerView = LayoutInflater.from(this).inflate(R.layout.question_detail_header, null);
        question_detail_header_questioner_name = (TextView) headerView.findViewById(R.id.question_detail_header_questioner_name);
        question_detail_header_questioner_ima = (RoundedImageView) headerView.findViewById(R.id.question_detail_header_questioner_ima);
        question_detail_header_questioner_detail = (TextView) headerView.findViewById(R.id.question_detail_header_questioner_detail);
        question_detail_header_questioner_content = (TextView) headerView.findViewById(R.id.question_detail_header_questioner_content);
        question_detail_header_answer_ima = (ImageView) headerView.findViewById(R.id.question_detail_header_answer_ima);
        question_detail_header_answer_content = (TextView) headerView.findViewById(R.id.question_detail_header_answer_content);
        question_detail_header_zan_layout = (LinearLayout) headerView.findViewById(R.id.question_detail_header_zan_layout);
        question_detail_header_answer_vip_ima1 = (ImageView) headerView.findViewById(R.id.question_detail_header_answer_vip_ima1);
        question_detail_header_answer_vip_ima = (ImageView) headerView.findViewById(R.id.question_detail_header_answer_vip_ima);
        question_detail_header_comment_layout = (LinearLayout) headerView.findViewById(R.id.question_detail_header_comment_layout);
        question_detail_header_zan = (TextView) headerView.findViewById(R.id.question_detail_header_zan);
        question_detail_header_comment = (TextView) headerView.findViewById(R.id.question_detail_header_comment);
        question_detail_header_answer_ima1 = (ImageView) headerView.findViewById(R.id.question_detail_header_answer_ima1);
        question_detail_header_answer_name = (TextView) headerView.findViewById(R.id.question_detail_header_answer_name);
        question_detail_header_answer_introduce = (TextView) headerView.findViewById(R.id.question_detail_header_answer_introduce);
        question_detail_header_answer_more = (TextView) headerView.findViewById(R.id.question_detail_header_answer_more);
        question_detail_header_answer_no_comment = (TextView) headerView.findViewById(R.id.question_detail_header_answer_no_comment);
        question_detail_header_price_layout = (LinearLayout) headerView.findViewById(R.id.question_detail_header_price_layout);
        question_detail_header_answer_content_pay = (TextView) headerView.findViewById(R.id.question_detail_header_answer_content_pay);
        question_detail_header_answer_layout = (LinearLayout) headerView.findViewById(R.id.question_detail_header_answer_layout);

        question_detail_header_questioner_ima.setOnClickListener(this);
        question_detail_header_answer_ima.setOnClickListener(this);
        question_detail_header_answer_ima1.setOnClickListener(this);
        question_detail_header_zan_layout.setOnClickListener(this);
        question_detail_header_comment_layout.setOnClickListener(this);
        question_detail_header_answer_content_pay.setOnClickListener(this);
        question_detail_header_answer_layout.setOnClickListener(this);

        arrayList = new ArrayList<>();
        questionDetailEvaluateAdapter = new QuestionDetailEvaluateAdapter(this, arrayList);
        if (question_detail_lv.getHeaderViewsCount() == 0) {
            question_detail_lv.addHeaderView(headerView);
        }
        question_detail_lv.setAdapter(questionDetailEvaluateAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                setReturn();
                break;
            case R.id.common_search:
                break;
            case R.id.question_detail_comment_layout:
                //评论之
                if (hasLoad()) {
                    Intent intent = new Intent(QuestionDetail.this, DeliverQuestionComment.class);
                    intent.putExtra("dialogId", dialogId);
                    startActivityForResult(intent, 0);
                } else {
                    this.login(this);
                }
                break;
//            case R.id.question_detail_share:
//                //分享
//                break;
            case R.id.question_detail_header_zan_layout:
                //赞
                PriseDialog();
                break;
            case R.id.question_detail_header_comment_layout:
                //进入评论页
                break;
            case R.id.question_detail_header_questioner_ima:
                //查看提问者信息
                Intent i = new Intent(QuestionDetail.this, PersonalPage.class);
                i.putExtra("userId", detail.getQuestioner_id());
                i.putExtra("type", detail.getQuestioner_from());
                startActivity(i);
                break;
            case R.id.question_detail_header_answer_ima:
            case R.id.question_detail_header_answer_ima1:
            case R.id.question_detail_header_answer_layout:
                //查看志愿者头像
                Intent i2 = new Intent(QuestionDetail.this, Volunteer.class);
                i2.putExtra("type", detail.getAnswerer_type());
                i2.putExtra("id", detail.getAnswerer_id());
                startActivity(i2);
                break;

            case R.id.question_detail_header_answer_content_pay:
                //给钱
                Intent i1 = new Intent(this, Pay.class);
                i1.putExtra("dialog_id", dialogId);
                i1.putExtra("price", Constant.QUESTIONWATCH_PRICE);
                i1.putExtra("payType", Constant.QUESTIONWATCH);
                startActivityForResult(i1, 2);
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        setReturn();
    }

    private void getDataFromInterNet(boolean waitingDialog) {
        HashMap<String, String> hashMap = new HashMap<>();
        if (!TextUtils.isEmpty(dialogId)) {
            if (!TextUtils.isEmpty(type)) {
                hashMap.put("type", type);
            }
            hashMap.put("dialogid", dialogId);
            new InterNetController(this, Constant.DIALOGDETAIL, handler, hashMap, GETDATA, waitingDialog);
        }
    }

    private void PriseDialog() {
        HashMap<String, String> hashMap = new HashMap<>();
        if (!TextUtils.isEmpty(dialogId)) {
            hashMap.put("dialog_id", dialogId);
            new InterNetController(this, Constant.DIALOGPRISE, handler, hashMap, ZAN, false);
        }
    }

    private void setReturn() {
        //设置返回给问题列表的信息
        Intent i = new Intent();
        i.putExtra("zan", zan);
        i.putExtra("comment", comment);
        i.putExtra("isPraised", isPraised);
        i.putExtra("hasPay", hasPay);
        setResult(1, i);
        QuestionDetail.this.finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_CANCELED) {
            if (resultCode == 1) {
                //有发表问题评论
                getDataFromInterNet(false);
            }
            if (resultCode == Constant.PAY_SUCCESS) {
                getDataFromInterNet(false);
            }
        }
    }
}
